//聊天
var TalkCon = iClass.set("Talk", function(talk_info) {
	this._id = TalkCon.getUuid();
	this.initInfo(talk_info);
	this.setInfo(talk_info);

	this.create_time = +new Date;
	db.insert(this.dbname, this);
	midEvent.emit("new_talk", this._id);
});
var FriendCon = iClass.get("Friend");
var UserCon = iClass.get("User");
TalkCon.getTalkListByFriendId = function(friend_id, num, page) {

	var friend_a = FriendCon.get(friend_id);
	if (!friend_a) {
		Throw("找不到指定好友关系")
	}

	var talk_list_a = db.find_list(TalkCon.dbname, {
		friend_id: friend_id
	});

	var friend_b = FriendCon.findUserFriend(friend_a.friend_id, friend_a.user_id);
	if (friend_b) {
		var talk_list_b = db.find_list(TalkCon.dbname, {
			friend_id: friend_b._id
		});
	} else {
		talk_list_b = [];
	}
	var talk_list = talk_list_a.concat(talk_list_b);

	//处理参数
	var total_num = talk_list.length;
	num = ~~num;
	num > 0 || (num = total_num); //num > 0
	page = ~~page;
	page >= 0 || (page = 0); //page >= 0
	var total_page = Math.ceil(total_num / num) || 1;
	//整理数据
	talk_list = talk_list.sortBy("create_time", true).page(num, page).reverse().map(function(talk) {
		talk = TalkCon.getInstance(talk);
		return talk;
	});
	return {
		total_num: total_num,
		total_page: total_page,
		num: num,
		page: page,
		list: talk_list,
		friend_id: friend_id,
		user_friend: UserCon.get(friend_a.friend_id)
	};
};